<?php

class Myzen_View_Helper_DateFormatter extends Zend_View_Helper_Abstract {
    
    public function dateFormatter($unixTime) {
		$timeMode = $this->dateMode($unixTime);
		switch ($timeMode['mode']) {
			case 'seconds':
				return ($timeMode['gap'] == 0)?'right now':($timeMode['gap'] == 1)?'1 second ago':$timeMode['gap'].' seconds ago';
				break;
			case 'minutes':
				return ($timeMode['gap'] == 1)?'1 minute ago':$timeMode['gap'].' minutes ago';
			break;
			case 'hours':
				return ($timeMode['gap'] == 1)?'about 1 hour ago':'about '.$timeMode['gap'].' hours ago';
			break;
			case 'date':
				return $timeMode['day'].'/'.$timeMode['month'].'/'.$timeMode['year'].' '.$timeMode['hours'].':'.$timeMode['minutes'];
			break;
		}
	
	} //end dateFormatter
	
	public function dateMode($unixTime) {
    	//We check the difference between current time and unix time
		$difference = time() - $unixTime;
		
		//Thirst case, difference is under 60 seconds, we display the time in seconds
		if($difference <60) return array('mode'=>'seconds','gap' =>$difference);
		//second case, difference is under 1 hour, we display the time in minutes
		elseif($difference <3600) return array('mode'=>'minutes','gap' =>floor($difference/60));
		//third case, difference is under 12 hour, we display the time in hours
		elseif($difference <43200) return array('mode'=>'hours','gap' =>floor($difference/3600));
		//Last use case we return the date
		else{
		//we compute the different components of the date
			$year = strftime(  '%Y',  $unixTime );
			$month = strftime(  '%m',  $unixTime );
			$day = strftime(  '%d',  $unixTime );
			$hours = strftime(  '%H',  $unixTime );
			$minutes = strftime(  '%M',  $unixTime );
			
			return array(
				'mode'=>'date', 
				'year' => $year, 
				'month' => $month, 
				'day' => $day, 
				'hours' => $hours, 
				'minutes' => $minutes, 				
				);
		}

		
    } //end datemode
} //end class